home *** CD-ROM | disk | FTP | other *** search
/ Power Tools for Macintosh / Power Tools for Macintosh (SoftBit)(1992).iso / Stacks / *F-I / HyperCard Utilities / SendSerial ƒ / SendSerial.p < prev    next >
Encoding:
Text File  |  1987-05-08  |  5.2 KB  |  250 lines  |  [TEXT/MPS ]

  1. {$R-}
  2. {$D+}
  3. (*
  4.     SendSerial --     a WildCard user-defined command 
  5.                     send bytes out the serial port (at specified baud rate).
  6.                     
  7.     example 1:
  8.         SendSerial ">1200 AX4500"
  9.         
  10.         sends the string AX4500 out the modem port at 1200 baud.
  11.         If no baud rate is specified it defaults to 9600.
  12.         
  13.     example 2:
  14.         SendSerial "AX4500^0D"
  15.         
  16.         sends the string AX4500<CR> out the modem port at 9600 baud.
  17.         The ^ indicates two hex didgits to follow. (Two ^^ means ^)
  18.  
  19.     To compile and link this file using Macintosh Programmer's Workshop,
  20.  
  21.     pascal SendSerial.p
  22.     link -o Test -sn Main=SendSerial -sn STDIO=SendSerial ∂
  23.          -sn INTENV=SendSerial -rt WCMD=111 ∂
  24.          SendSerial.p.o {MPW}libraries:interface.o
  25.  
  26.     then use ResEdit to copy the resulting WCMD from Test
  27.     and paste it into WildCard, the Home stack, or your own stack.
  28. *)
  29.  
  30. UNIT DummyUnit;
  31.  
  32. INTERFACE
  33.  
  34.    USES MemTypes, QuickDraw, OsIntf;
  35.     
  36. IMPLEMENTATION
  37.  
  38. const debug = false;
  39.  
  40. PROCEDURE SendSerial(commandPtr: Ptr);                            FORWARD;
  41.  
  42.    PROCEDURE EntryPoint(arg: Ptr);
  43.    { entry point cannot have local procs, but forward routines can }
  44.    BEGIN
  45.      SendSerial(arg);
  46.    END;
  47.  
  48.    PROCEDURE SendSerial(commandPtr: Ptr);
  49.    VAR reverseFlag, offFlag, tillFlag: BOOLEAN;
  50.        message, tempStr: Str255;
  51.        refNum: INTEGER;
  52.        err: INTEGER;
  53.        baudRate: INTEGER;
  54.             
  55.      Procedure Error;
  56.      BEGIN
  57.         SysBeep(1);
  58.         Exit (SendSerial);
  59.      END;
  60.         
  61.      PROCEDURE OpenSerial;
  62.      VAR handShake: SerShk;
  63.           bRate: INTEGER;
  64.      BEGIN
  65.        { for now, use modem port so we don't mess with AppleTalk }
  66.        bRate := baudRate;
  67.        err := FSOpen('.AOUT',0,refNum);
  68.        IF err = 0 THEN 
  69.          BEGIN
  70.            WITH handShake DO
  71.              BEGIN
  72.                fXon := 1;
  73.                fCTS := 1;
  74.                xon  := CHR(17);
  75.                xoff := CHR(19);
  76.                errs := 0;
  77.                evts := 0;
  78.                fInx := 0;
  79.              END;
  80.            err := SerHShake(refNum,handShake);
  81.            IF err = 0 THEN 
  82.              err := Control(refNum,13,@bRate);
  83.          END;
  84.         if debug then 
  85.             BEGIN
  86.             MoveTo (150,30);
  87.             DrawString('Opened Serial');
  88.             end;
  89.      END;
  90.      
  91.      
  92.      PROCEDURE CloseSerial;
  93.      BEGIN
  94.        err := FSClose(refNum);
  95.         if debug then 
  96.             BEGIN
  97.             MoveTo (150,90);
  98.             DrawString('closed Serial');
  99.             end;
  100.      END;
  101.      
  102.      
  103.      PROCEDURE SendCommand(cmd: Str255);
  104.      VAR count: LongInt;
  105.      BEGIN
  106.         if debug then 
  107.             BEGIN
  108.             MoveTo (150,60);
  109.             DrawString('About to FSWrite');
  110.             end;
  111.        count := Length(cmd);
  112.        err := FSWrite(refNum, count, Pointer(Ord(@cmd)+1));
  113.         if debug then 
  114.             BEGIN
  115.             MoveTo (250,60);
  116.             DrawString('FSWrote');
  117.             end;
  118.      END;
  119.  
  120.      PROCEDURE GetMessage;     
  121.      VAR charNum: INTEGER;
  122.          msgChar: CHAR;
  123.          
  124.          PROCEDURE SetBaudRate;
  125.              VAR ch: CHAR;
  126.          BEGIN
  127.            baudRate := 0;
  128.            ch := CHR(commandPtr^);
  129.            if debug then MoveTo(50,100);
  130.            WHILE ch <> ' ' DO
  131.                    BEGIN
  132.                 if debug then
  133.                     DrawChar(ch);
  134.                 IF (ch < '0') OR (ch > '9') THEN
  135.                     Error;
  136.                 baudRate := 10*baudRate + ORD(CHR(commandPtr^)) - ORD('0');
  137.                 commandPtr := Pointer(Ord(commandPtr)+1);
  138.                    ch := CHR(commandPtr^);
  139.                 END;
  140.          END;
  141.           
  142.          FUNCTION GetHex: CHAR;
  143.              VAR ch: CHAR;
  144.                 hex: INTEGER;
  145.          BEGIN
  146.              ch := CHR(commandPtr^);
  147.             IF ch = '^' THEN {two ^'s means really want a ^}
  148.                 GetHex := '^'
  149.              ELSE 
  150.                 BEGIN
  151.                 IF (ch >= '0') AND (ch <= '9') THEN
  152.                     hex := ORD(ch) - ORD('0')
  153.                 ELSE IF (ch >= 'a') AND (ch <= 'f') THEN 
  154.                     hex := 10 + ORD(ch) - ORD('a')
  155.                 ELSE IF (ch >= 'A') AND (ch <= 'F') THEN 
  156.                     hex := 10 + ORD(ch) - ORD('A')
  157.                 ELSE
  158.                     Error;
  159.                     
  160.                    commandPtr := Pointer(Ord(commandPtr)+1);
  161.  
  162.                 ch := CHR(commandPtr^);
  163.                 IF (ch >= '0') AND (ch <= '9') THEN
  164.                     hex := 16*hex + ORD(ch) - ORD('0')
  165.                 ELSE IF (ch >= 'a') AND (ch <= 'f') THEN 
  166.                     hex := 16*hex + 10 + ORD(ch) - ORD('a')
  167.                 ELSE IF (ch >= 'A') AND (ch <= 'F') THEN 
  168.                     hex := 16*hex + 10 + ORD(ch) - ORD('A')
  169.                 ELSE
  170.                     Error;
  171.                 
  172.                 GetHex := CHR(hex);
  173.                 END;
  174.                commandPtr := Pointer(Ord(commandPtr)+1);
  175.          END;
  176.           
  177.      BEGIN
  178.        { skip command name }
  179.        WHILE (commandPtr^ <> 0) AND (commandPtr^ <> 13) AND (CHR(commandPtr^) <> ' ') DO
  180.          commandPtr := Pointer(Ord(commandPtr)+1);
  181.          
  182.        { skip following white space }
  183.        WHILE CHR(commandPtr^) = ' ' DO 
  184.          commandPtr := Pointer(Ord(commandPtr)+1);
  185.          
  186.        { see if baud rate specified }
  187.        IF CHR(commandPtr^) = '>' THEN
  188.             BEGIN
  189.          commandPtr := Pointer(Ord(commandPtr)+1);
  190.          IF CHR(commandPtr^) <> '>' THEN
  191.              BEGIN
  192.              SetBaudRate;
  193.                { skip following white space }
  194.                WHILE CHR(commandPtr^) = ' ' DO 
  195.                  commandPtr := Pointer(Ord(commandPtr)+1);
  196.             END;
  197.          END;
  198.             
  199.          
  200.        { extract the rest into a Str255 }
  201.        charNum := 0;
  202.        WHILE (commandPtr^ <> 0) AND (charNum < 255) DO
  203.          BEGIN
  204.            msgChar := CHR(commandPtr^);
  205.            commandPtr := Pointer(Ord(commandPtr)+1);
  206.            charNum := charNum + 1;
  207.            IF msgChar = '^' THEN
  208.                    msgChar := GetHex;
  209.            message[charNum] := msgChar;
  210.          END;
  211.          
  212.        message[0] := CHR(charNum);
  213.        
  214.      if debug then
  215.          begin
  216.         moveTo(50,140);
  217.         drawstring(message);
  218.         end;
  219.      END;
  220.      
  221.        
  222.  
  223.    BEGIN {SendSerial}
  224.         baudRate := 9600;
  225.      GetMessage;
  226.      
  227.      OpenSerial;
  228.      IF err <> 0 THEN 
  229.        BEGIN
  230.          SysBeep(1);
  231.          EXIT(SendSerial);
  232.        END;
  233.      
  234.      SendCommand(message);
  235.           
  236.      CloseSerial;
  237.      
  238.      if debug then
  239.          begin
  240.         moveTo(50,180);
  241.         drawstring('Finis');
  242.         end;
  243.  
  244.    END;   
  245.  
  246. END.
  247.  
  248.  
  249.  
  250.